/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.own.entity.OwnOutInformation;
import com.jeesite.modules.own.service.OwnMedicineAssignmentService;
import com.jeesite.modules.own.service.OwnOutInformationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnOutContents;
import com.jeesite.modules.own.service.OwnOutContentsService;

import java.util.List;

/**
 * 出库信息目录Controller
 * @author F
 * @version 2019-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownOutContents")
public class OwnOutContentsController extends BaseController {

	@Autowired
	private OwnOutContentsService ownOutContentsService;

	@Autowired
	private OwnOutInformationService ownOutInformationService;

	@Autowired
	private OwnMedicineAssignmentService ownMedicineAssignmentService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnOutContents get(String id, boolean isNewRecord) {
		return ownOutContentsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownOutContents:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnOutContents ownOutContents, Model model) {
		model.addAttribute("ownOutContents", ownOutContents);
		return "modules/own/ownOutContentsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownOutContents:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnOutContents> listData(OwnOutContents ownOutContents, HttpServletRequest request, HttpServletResponse response) {
		ownOutContents.setPage(new Page<>(request, response));
		Page<OwnOutContents> page = ownOutContentsService.findPage(ownOutContents);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownOutContents:view")
	@RequestMapping(value = "form")
	public String form(OwnOutContents ownOutContents, Model model) {
		model.addAttribute("ownOutContents", ownOutContents);
		return "modules/own/ownOutContentsForm";
	}

	/**
	 * 保存出库信息目录
	 */
	@RequiresPermissions("own:ownOutContents:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnOutContents ownOutContents) {
		ownOutContentsService.save(ownOutContents);
		if (ownOutContents.getOwnOutInformationList() != null && ownOutContents.getIsNewRecord()){
			List<OwnOutInformation> ownOutInformations = ownOutContents.getOwnOutInformationList();
			for (OwnOutInformation ownOutInformation: ownOutInformations){
				ownOutInformation.setContentsId(ownOutContents.getId());
				ownMedicineAssignmentService.updateOutStatus(ownOutInformation.getOwnMedicineAssignmentId(), "1");
				ownOutInformationService.save(ownOutInformation);
			}
		}
		return renderResult(Global.TRUE, text("保存出库信息成功！"), ownOutContents);
	}
	
	/**
	 * 删除出库信息目录
	 */
	@RequiresPermissions("own:ownOutContents:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnOutContents ownOutContents) {
		ownOutContentsService.delete(ownOutContents);
		return renderResult(Global.TRUE, text("删除出库信息成功！"));
	}
	
}